<?php

namespace App\Repositories;

use App\Models\Company;

/**
 * 公司模块Repository
 *
 * Date: 22/03/2018
 * @author George
 * @package App\Repositories
 */
class CompanyRepository extends BaseRepository
{
	/**
	 * 定义模型
	 *
	 * Date: 22/03/2018
	 * @author George
	 * @return mixed|string
	 */
	public function model()
	{
		return Company::class;
	}

	/**
	 * 根据公司信息获取模块
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param $company
	 * @return \Illuminate\Database\Eloquent\Model|null|object|static
	 */
	public function getModules($company)
	{
		$modules = [];

		if (is_integer($company)) {
			$result = $this->with('modules')->where('id', $company)->first();
			$modules = optional($result)->modules;
		} elseif ($company instanceof Company) {
			$modules = $company->modules()->get();
		}

		return $modules;
	}

	/**
	 * 修改公司状态
	 *
	 * Date: 23/03/2018
	 * @author George
	 * @param $company
	 * @param string $status
	 * @return BaseRepository|bool|\Illuminate\Database\Eloquent\Model|mixed|null|object
	 * @throws \App\Exceptions\GeneralException
	 * @throws \Throwable
	 */
	public function modifyStatus($company, $status = 'accepted')
	{
		if (is_integer($company)) {
			$company = $this->updateByPrimary($company, ['status' => 'accepted']);
		} elseif ($company instanceof Company) {
			$company->status = $status;
			$company = $company->saveOrFail();
		}
		return $company;
	}

	/**
	 * 获取公司信息
	 *
	 * Date: 27/04/2018
	 * @author George
	 * @param bool $city
	 * @return \Illuminate\Database\Eloquent\Collection|static[]
	 */
	public function getByCity($city = false)
	{
		$query = Company::query()->select(['id', 'title', 'city']);
		if ($city) {
			$query->where('city', $city);
		}
		return $query->get();
	}

	protected function serialization(array $attributes)
	{
		// TODO: Implement serialization() method.
	}
}
